package com.autohelper.app.tcp.cmd;

import com.autohelper.app.ActionOperator;
import com.autohelper.app.ServiceHolder;
import com.google.gson.Gson;

import java.util.HashMap;

public class RequestRouter {
    static Gson gson = new Gson();

    public static void route(short cmd, String params, ActionOperator.IActionCallback callback) {
        CmdEnum cmdEnum = CmdEnum.getByCode(cmd);
        if (cmdEnum == null) {
            callback.fail("cmd错误");
            return;
        }
        HashMap absParam = gson.fromJson(params, HashMap.class);
        String note = absParam.getOrDefault("note", "").toString();
        if (!"".equalsIgnoreCase(note)) {
            ServiceHolder.INSTANCE.getFloatingStepView().updateStepText(note);
        }

        if (cmdEnum == CmdEnum.START_APP) {
            StartAppCmdParam packet = gson.fromJson(params, StartAppCmdParam.class);
            ServiceHolder.INSTANCE.getActionOperator().launchApp(packet.getPackageName(), packet.getActivityName(), callback);
        } else if (cmdEnum == CmdEnum.SWIPE) {
            SwipeCmdParam packet = gson.fromJson(params, SwipeCmdParam.class);
            callback.success(ServiceHolder.INSTANCE.getActionOperator().swipe(packet.getStartX(), packet.getStartY(), packet.getEndX(), packet.getEndY(), packet.getDuration()));
        } else if (cmdEnum == CmdEnum.INPUT) {
            InputCmdParam packet = gson.fromJson(params, InputCmdParam.class);
            callback.success(ServiceHolder.INSTANCE.getActionOperator().inputText(packet.getText()));
        } else if (cmdEnum == CmdEnum.INPUT_KEY) {
            InputKeyCmdParam packet = gson.fromJson(params, InputKeyCmdParam.class);
            callback.success(ServiceHolder.INSTANCE.getActionOperator().inputKey(packet.getKeycode()));
        } else if (cmdEnum == CmdEnum.CLICK) {
            ClickCmdParam packet = gson.fromJson(params, ClickCmdParam.class);
            callback.success(ServiceHolder.INSTANCE.getActionOperator().click(packet.getX(), packet.getY()));
        } else if (cmdEnum == CmdEnum.UI_XML) {
            callback.success(ServiceHolder.INSTANCE.getActionOperator().dumpWindowHierarchy());
        } else if (cmdEnum == CmdEnum.WM_SIZE) {
            callback.success(ServiceHolder.INSTANCE.getActionOperator().getWmSize());
        } else if (cmdEnum == CmdEnum.SCREEN_CAPTURE) {
            ServiceHolder.INSTANCE.getActionOperator().screenCapture(callback);
        } else if (cmdEnum == CmdEnum.GO_HOME) {
            callback.success(ServiceHolder.INSTANCE.getActionOperator().goHome());
        } else if (cmdEnum == CmdEnum.GO_BACK) {
            callback.success(ServiceHolder.INSTANCE.getActionOperator().goBack());
        } else if (cmdEnum == CmdEnum.CLEAR_TEXT) {
            callback.success(ServiceHolder.INSTANCE.getActionOperator().clearText());
        } else if (cmdEnum == CmdEnum.GET_CLIPBOARD_CONTENT) {
            GetClipboardContentCmdParam param = gson.fromJson(params, GetClipboardContentCmdParam.class);
            ServiceHolder.INSTANCE.getActionOperator().getClipboardContent(callback, param.getPackageName(), param.getActivityName());
        } else if (cmdEnum == CmdEnum.SET_CLIPBOARD_CONTENT) {
            callback.fail("功能未实现");
        }
    }
}
